package com.news.collect.common.utils.http;

import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.util.Map;

public class HttpUtils {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class);

    /**
     * 发送Get
     *
     * @param url
     * @param param
     * @param contentType
     * @return
     */
    public static String sendGet(String url, Map<String, String> param, String contentType) {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try {
            String urlNameString = url;
            if (param != null && param.size() > 0) {
                urlNameString += "?";
                for (Map.Entry<String, String> entry : param.entrySet()) {
                    urlNameString += String.format("%s=%s&&", entry.getKey(), entry.getValue());
                }
            }
            urlNameString = urlNameString.replace(urlNameString.charAt(urlNameString.length() - 1) + "", "");
            LOGGER.info("sendGet - {}", urlNameString);
            URL actualUrl = new URL(urlNameString);
            HttpURLConnection connection = (HttpURLConnection) actualUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
            String strLine = "";
            while ((strLine = reader.readLine()) != null) {
                sb.append(strLine);
            }
            LOGGER.info("receive - {}", sb);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException exception) {
            exception.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return sb.toString();
    }

    public static String sendGet(String url, Map param) {
        return sendGet(url, param, "UTF-8");
    }

    /**
     * 发送Post
     * @param url
     * @param param
     * @param contentType
     * @return
     */
    public static String sendPost(String url, String param, String contentType) {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try {
            URL actualUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) actualUrl.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            // 设置传入参数的格式
            connection.setRequestProperty("Content-Type", contentType);
            OutputStream os = connection.getOutputStream();
            os.write(param.getBytes());
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String strLine = "";
            while ((strLine = reader.readLine()) != null) {
                sb.append(strLine);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException exception) {
            exception.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return sb.toString();
    }
}
